package lzyc.pointsManagement.business.activity.convertor;


import lzyc.pointsManagement.business.activity.domain.ActivityApply;
import lzyc.pointsManagement.business.activity.dto.ActivityApplyDto;
import lzyc.pointsManagement.common.core.domain.entity.SysUser;
import lzyc.pointsManagement.common.utils.ShiroUtils;
import lzyc.pointsManagement.common.utils.StringUtils;
import lzyc.pointsManagement.common.utils.spring.SpringUtils;
import lzyc.pointsManagement.common.utils.uuid.IdUtils;
import lzyc.pointsManagement.system.service.ISysUserService;

import java.util.Date;

public class ActivityApplyConvertor {
    public static ActivityApplyDto convertToDto(ActivityApply entity) {
        ActivityApplyDto dto = new ActivityApplyDto();
        if (entity != null) {
            dto.setId(entity.getId());
            dto.setUserid(entity.getUserid());
            ISysUserService iSysUserService = SpringUtils.getBean(ISysUserService.class);
            SysUser sysUser = iSysUserService.selectUserByLoginName(entity.getUserid());
            if (sysUser != null) {
                dto.setUserInfo(sysUser);
            }
            dto.setStatus(entity.getStatus());
            dto.setCreateTime(entity.getCreateTime());
            dto.setActivityId(entity.getActivityId());
            dto.setApprovalUserId(entity.getApprovalUserId());
            dto.setApprovalTime(entity.getApprovalTime());
            dto.setTitle(entity.getTitle());
            dto.setDescription(entity.getDescription());
        }
        return dto;
    }
    public static ActivityApply convertToEntity(ActivityApplyDto dto) {
        ActivityApply entity = new ActivityApply();
        if (dto != null) {
            if (StringUtils.isEmpty(dto.getId())) {
                entity.setId(IdUtils.randomUUID());
                dto.setId(entity.getId());
            } else {
                entity.setId(dto.getId());
            }
            entity.setUserid(ShiroUtils.getLoginName());
            entity.setStatus(dto.getStatus());
            if (dto.getCreateTime() != null) {
                entity.setCreateTime(dto.getCreateTime());
            } else {
                entity.setCreateTime(new Date());
            }
            entity.setActivityId(dto.getActivityId());
            entity.setApprovalUserId(dto.getApprovalUserId());
            entity.setTitle(dto.getTitle());
            entity.setDescription(dto.getDescription());
            if (dto.getApprovalTime() != null) {
                entity.setApprovalTime(dto.getApprovalTime());
            } else {
                entity.setApprovalTime(new Date());
            }
        }
        return entity;
    }

}
